package com.example.bourse.core.result;

import com.example.bourse.core.enums.ReqCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel(description = "API 返回结果")
public class ApiResult<T> {
    @ApiModelProperty(value = "状态码", example = "200")
    private int code;
    @ApiModelProperty(value = "返回数据", example = "{}")
    private T data;
    @ApiModelProperty(value = "消息", example = "成功")
    private String msg;

    public ApiResult() {}

    public ApiResult(int code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    public static <T> ApiResult<T> success(T data) {
        return new ApiResult<>(ReqCodeEnum.SUCCESS.getCode(), data, ReqCodeEnum.SUCCESS.getMessage());
    }

    public static <T> ApiResult<T> success() {
        return new ApiResult<>(ReqCodeEnum.SUCCESS.getCode(), null, ReqCodeEnum.SUCCESS.getMessage());
    }

    public static <T> ApiResult<T> error(String errorCode) {
        return new ApiResult<>(ReqCodeEnum.CUSTOM_EXCEPTION.getCode(), null, errorCode);
    }

    public static <T> ApiResult<T> fail(String message) {
        return new ApiResult<>(ReqCodeEnum.CUSTOM_EXCEPTION.getCode(), null, message);
    }

    public static <T> ApiResult<T> error(Integer code, String errorCode) {
        return new ApiResult<>(code, null, errorCode);
    }

    public static <T> ApiResult<T> error() {
        return new ApiResult<>(ReqCodeEnum.ERROR.getCode(), null, ReqCodeEnum.ERROR.getMessage());
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}
